﻿using FrmMain.Util;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WindowTime.Util;
using static FrmMain.Util.Enums;

namespace FrmMain
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
            var enumList = EnumExtension.GetEnums<Time>();
            cbbTime.DisplayMember = "Key";
            cbbTime.ValueMember = "Value";
            cbbTime.DataSource = enumList;

            cbbMode.DisplayMember = "Key";
            cbbMode.ValueMember = "Value";
            cbbMode.DataSource = EnumExtension.GetEnums<ProcessType>();

            try
            {
                if (Environment.OSVersion.Version.Major < 6)
                {
                    base.SendToBack();

                    IntPtr hWndNewParent = User32.FindWindow("Progman", null);
                    User32.SetParent(base.Handle, hWndNewParent);
                }
                else
                {
                    User32.SetWindowPos(base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
                }
            }
            catch (ApplicationException exx)
            {
                MessageBox.Show(this, exx.Message, "Pin to Desktop");
            }

        }

        /// <summary>
        /// 关机
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                var seconds = Convert.ToInt64(cbbTime.SelectedValue);
                if (seconds < 0)
                {
                    MessageBox.Show("时间不可小于0", "提示");
                }
                if (cbbMode.SelectedValue.ToString() == ((int)ProcessType.关机).ToString())
                {
                    var process = Process.Start("shutdown", "-s -f -t " + seconds.ToString());//执行关机指令
                }
                else if (cbbMode.SelectedValue.ToString() == ((int)ProcessType.重启).ToString())
                {
                    var process = Process.Start("shutdown", "-r -t " + seconds.ToString());//执行重启指令
                }
                MessageBox.Show(cbbTime.Text + "后" + cbbMode.Text, "提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 启动cmd
        /// </summary>
        /// <param name="cmdCommand">cmd指令</param>
        public void ProcessStart(string cmdCommand)
        {
            Process da = new Process();
            da.StartInfo.FileName = "cmd.exe";
            da.StartInfo.UseShellExecute = false;
            da.StartInfo.RedirectStandardInput = true;
            da.StartInfo.RedirectStandardOutput = true;
            da.StartInfo.RedirectStandardError = true;
            da.StartInfo.CreateNoWindow = true;
            da.OutputDataReceived += OutputDataReceived;//返回事件
            da.Start();
            da.BeginOutputReadLine();
            da.StandardInput.WriteLine(cmdCommand);
        }

        /// <summary>
        /// 返回事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            this.BeginInvoke(new Action(() =>
            {
                if (!e.Data.Contains("Microsoft Windows") && !e.Data.Contains("保留所有权利") && !e.Data.Contains(@"C:\Windows\system32") && !string.IsNullOrWhiteSpace(e.Data) && !e.Data.Contains("shutdown"))
                    MessageBox.Show(e.Data);
            }));


        }

        /// <summary>
        /// 取消关机/重启
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            Process.Start("shutdown", "/a");
        }

        private void FrmMain_Activated(object sender, EventArgs e)
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                User32.SetWindowPos(base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
            }
        }

        private void FrmMain_Paint(object sender, PaintEventArgs e)
        {
            if (Environment.OSVersion.Version.Major >= 6)
            {
                User32.SetWindowPos(base.Handle, 1, 0, 0, 0, 0, User32.SE_SHUTDOWN_PRIVILEGE);
            }
        }

        /// <summary>
        /// 休眠
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            PowrProf.SetSuspendState(true, true, true);
        }

        /// <summary>
        /// 睡眠
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            PowrProf.SetSuspendState(false, true, true);
        }

        /// <summary>
        /// 锁定
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            User32.LockWorkStation();
        }

        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            User32.ExitWindowsEx(0, 0);
        }

        public const uint WM_SYSCOMMAND = 0x112; //系统消息
        public const int SC_MONITORPOWER = 0xF170; //关闭显示器的系统命令
        public const int MonitorPowerOff = 2; //2为PowerOff, 1为省电状态，-1为开机
        public static readonly IntPtr HWND_BROADCAST = new IntPtr(0xffff);//广播消息，所有顶级窗体都会接收
        private void toolStripButton5_Click(object sender, EventArgs e)
        {
            User32.SendMessage(HWND_BROADCAST, WM_SYSCOMMAND, SC_MONITORPOWER, MonitorPowerOff);
        }
    }
}
